package net.minecraft.src;

import java.util.ArrayList;

public class ItemScepter extends ItemTool {

	public String enchantment;
	public int maxEnchantment;
	public int charges;
	public int maxCharges;
	public static ArrayList sceptersList;
	
	protected ItemScepter(int i, EnumToolMaterial toolMaterial, Block aBlock[]) {
		super(i, 0, toolMaterial, null);
		if (toolMaterial.equals(EnumToolMaterial.WOOD)) {
			maxCharges = 16;
			setMaxDamage(16);
			maxEnchantment = 1;
		}
		else if (toolMaterial.equals(EnumToolMaterial.STONE)) {
			maxCharges = 32;
			setMaxDamage(32);
			maxEnchantment = 2;
		}
		else if (toolMaterial.equals(EnumToolMaterial.IRON)) {
			maxCharges = 48;
			setMaxDamage(48);
			maxEnchantment = 3;
		}
		else if (toolMaterial.equals(EnumToolMaterial.GOLD)) {
			maxCharges = 64;
			setMaxDamage(64);
			maxEnchantment = 3;
		}
		else if (toolMaterial.equals(EnumToolMaterial.EMERALD)) {
			maxCharges = 128;
			setMaxDamage(128);
			maxEnchantment = 4;
		}
		charges = 3;
		enchantment = "fireball";
	}
	
	public ItemScepter setEnchantment(String ench){
		enchantment = ench;
		return this;
	}
	
	public ItemScepter setCharges(int i) {
		charges = i;
		return this;
	}
	
	public ItemScepter useCharge() {
		charges--;
		
		return this;
	}

	@Override
	public float getStrVsBlock(ItemStack itemstack, Block block) {

		return 0;
//		return super.getStrVsBlock(itemstack, block);
	}

	@Override
	public ItemStack onItemRightClick(ItemStack itemstack, World world,
			EntityPlayer entityplayer) {
		entityplayer.swingItem();
		
		if (enchantment != null) {
			itemstack.damageItem(1, entityplayer);
			doEnchantmentCast(world, entityplayer);
		}
		
		if (charges < 0) {
			enchantment = null;
		}
		
		return super.onItemRightClick(itemstack, world, entityplayer);
	}

	@Override
	public int getMaxDamage() {
		// TODO Auto-generated method stub
		return super.getMaxDamage();
	}

	@Override
	public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer,
			World world, int i, int j, int k, int l) {
		
//		InscriptionManager.checkInscription(i, j, k, world, entityplayer);
		
		if (enchantment != null) {
			doEnchantmentOnBlock(i,j,k,world,entityplayer);
		}
		return true;
	}

	@Override
	public boolean onBlockDestroyed(ItemStack itemstack, int i, int j, int k,
			int l, EntityLiving entityliving) {
		// TODO Auto-generated method stub
		return super.onBlockDestroyed(itemstack, i, j, k, l, entityliving);
	}

	@Override
	public int getDamageVsEntity(Entity entity) {
		// TODO Auto-generated method stub
		return super.getDamageVsEntity(entity);
	}
	
	public void doEnchantmentOnBlock(int x, int y, int z, World world, EntityPlayer ep) {
		InscriptionManager.checkInscription(x, y, z, world, ep);
		
		if(enchantment.equalsIgnoreCase("fireball")){
			if(world.getBlockId(x, y + 1, z) == 0 && Block.fire.canPlaceBlockAt(world, x, y, z)){
				world.setBlockWithNotify(x, y + 1, z, Block.fire.blockID);
			}
		}
		else if(enchantment.equalsIgnoreCase("lightning")){
			if(world.getBlockId(x, y + 1, z) == 0 && mod_Inscription.invisRedTorch.canPlaceBlockAt(world, x, y, z)){
				world.setBlockWithNotify(x, y + 1, z, mod_Inscription.invisRedTorch.blockID);
			}
		}
		else if (enchantment.equalsIgnoreCase("bulkMiner")) {
			Block block = Block.blocksList[world.getBlockId(x, y, z)];
			mod_Inscription.sendChatMessage("Block is " + block.getBlockName());
			if (block.getBlockName().startsWith("tile.ore")) {
				mod_Inscription.sendChatMessage("Bulk Mining.", 'a');
			}
		}
	}
	
	public void doEnchantmentCast(World world, EntityPlayer ep) {
		MovingObjectPosition mop = ep.rayTrace(1024D, 1F);
		
		
		if (enchantment.equalsIgnoreCase("fireball") && mop != null) {
//			ep.addChatMessage("Casting fireball.");
            double d5 = mop.hitVec.xCoord - ep.posX;
            double d6 = mop.hitVec.yCoord - ep.posY+ep.height/2;
            double d7 = mop.hitVec.zCoord - ep.posZ;
			EntityFireball entityfireball = new EntityFireball(world, ep, d5,d6,d7);
            Vec3D vec3d = ep.getLook(1.0F);
            entityfireball.posX = ep.posX + vec3d.xCoord;
            entityfireball.posY = ep.posY;
            entityfireball.posZ = ep.posZ + vec3d.zCoord;
            world.entityJoinedWorld(entityfireball);
		}
		else if (enchantment.equalsIgnoreCase("lightning") && mop != null) {
//			ep.addChatMessage("Casting lightning.");
			double d5 = mop.hitVec.xCoord - ep.posX;
            double d6 = mop.hitVec.yCoord - ep.posY+ep.height/2;
            double d7 = mop.hitVec.zCoord - ep.posZ;
            EntityLightningBolt entitylightning = new EntityLightningBolt(world, d5, d6, d7);
            Vec3D vec3d = ep.getLook(1.0F);
            entitylightning.posX += ep.posX + vec3d.xCoord;
            entitylightning.posZ += ep.posZ + vec3d.zCoord;
            //checks what yCoord the lightning should strike
            int yCheck = 1;
            while(!world.canBlockSeeTheSky((int)entitylightning.posX, yCheck, (int)entitylightning.posZ)) yCheck++;
//            ep.addChatMessage("Block can see sky : yCheck[" + yCheck + "]");
            entitylightning.posY = yCheck;
            world.entityJoinedWorld(entitylightning);
		}
		else if (mop == null){
			mod_Inscription.debug("Cannot cast spell.");
		}
	}
	
	public void doEnchantmentOnEntity(World world, EntityPlayer ep, EntityLiving target) {
//		ep.addChatMessage("Using enchantment on entity.");
	}
	
	static {
		sceptersList = new ArrayList<ItemScepter>();
		sceptersList.add(mod_Inscription.scepterGold);
		sceptersList.add(mod_Inscription.scepterDiamond);
		sceptersList.add(mod_Inscription.scepterIron);
		sceptersList.add(mod_Inscription.scepterStone);
		sceptersList.add(mod_Inscription.scepterWood);
	};

}
